unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, DB, nhibDataset, UMono, UMonoIntf, UVarMono,
  UMonoRTTIInv;

type
  TForm5 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    FDataSet: TNhibernateDataset;
  public
    { Public declarations }

  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

procedure myShowMessage(aValue: PMonoString); cdecl;
var
  sw: string;
begin
  sw := TMonoConvert.MonoStrToString(aValue);
  Dialogs.ShowMessage('MONO: '+sw);
end;

procedure TForm5.Button1Click(Sender: TObject);
var
  vObj: variant;
  vList: variant;
  i: integer;
begin
  vObj := VarMonoCreate(MonoJit.FindMonoClass('demo_05', 'MainClass'));
  vList := vObj.GetList();
  FDataSet.SourceList := vList;
  FDataset.Open;
  DataSource1.DataSet := FDataSet;
  for i := 0 to DBGrid1.Columns.Count-1 do
    if DBGrid1.Columns[i].Width > 150 then
       DBGrid1.Columns[i].Width := 150;
end;

procedure TForm5.Button2Click(Sender: TObject);
begin
  FDataSet.Close;
  FDataSet.SourceList := Null;
end;

procedure TForm5.Button3Click(Sender: TObject);
var
  vObj: variant;
  res: variant;
  s: string;
begin
  //SumFloat
  vObj := FDataSet.CurrentObject;
  res := vObj.SumFloat();
  s := 'SumFloat = '+FloatToStr(res);
  //DatePlusInt
  res := vObj.DatePlusInt();
  if VarIsNull(res) then
    s := s + ' DatePlusInt Null'
  else
    s := s + ' DatePlusInt '+DateTimeToStr(res);

  FDataSet.FieldByName('StrCol').AsString := s;
  // or
  //vObj.StrCol := s;
  //FDataset.Refresh;
end;

procedure TForm5.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown:= DebugHook <> 0;
  SetMonoPath('C:\Program Files\Mono-2.8.2\bin');
  InitMono;

  if OpenDialog1.Execute then
  begin
    MonoJit := TMonoJit.Create('v4.0.30319', OpenDialog1.FileName, '0', 0);

    MonoJit.AddInternalCall('Mono4Delphi','Dialogs','ShowMessage', @myShowMessage);
    MonoJit.AddInternalCall('Mono4Delphi','TProxyAnyIntf', 'DelphiInvoke', @DelphiRTTIInvoke);
    MonoJit.AddClassMap(TObject, 'Mono4Delphi.ITObject');
    MonoJit.AddClassMap(TComponent, 'Mono4Delphi.ITComponent');
    MonoJit.AddClassMap(TControl, 'Mono4Delphi.ITControl');
    MonoJit.AddClassMap(TForm, 'Mono4Delphi.ITForm');
    MonoJit.AddClassMap(TEdit, 'Mono4Delphi.ITEdit');
    MonoJit.AddClassMap(TButton, 'Mono4Delphi.ITButton');
    MonoJit.AddClassMap(TStrings, 'Mono4Delphi.ITStrings');
    MonoJit.AddClassMap(TMemo, 'Mono4Delphi.ITMemo');

    MonoJit.LoadAssembly;
  end;
  FDataSet := TNhibernateDataset.Create(self);
  MonoJit.Exec;  // init config
end;


initialization
finalization
  MonoJit.Free;
end.
